﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Data.Objects;
using System.Linq;
using System.Text;
using MeresModeling;

//using MeresModeling;

namespace Meres.Modeling.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            TestDb();
            
            //GetRandomText();
            //GenerateSampleDatabase();
        }

        const string ModuleDir = @"D:\HERO\LAB\eking\Modules\Meres\MeresModeling\";

        private static void TestDb()
        {

            var cs = ConfigurationManager.ConnectionStrings["MeresModel"].ConnectionString.Replace(
                "|ModuleDir|", ModuleDir).Replace("res://*",
                                                  "res://" + typeof(MeresModel).Assembly.FullName);

            var model = new MeresModel(cs);
            var dat = model.GetTableModels();
        }

        private static void GenerateSampleDatabase()
        {
            var cs = ConfigurationManager.ConnectionStrings["MeresModel"].ConnectionString.Replace(
                "|ModuleDir|", ModuleDir).Replace("res://*",
                                                  "res://" + typeof(MeresModel).Assembly.FullName);


            var model = new MeresModel(cs);


            // Clean
            var toDel = Enumerable.Cast<object>(model.MembershipGroups).ToList();
            toDel.AddRange(model.PersonCategories);
            toDel.AddRange(model.People.ToList());
            toDel.ForEach(model.DeleteObject);
            model.SaveChanges();


            // Group
            var mgs = new List<MembershipGroup> { new MembershipGroup { Name = "EKING SYSTEM" } };
            mgs.Add(new MembershipGroup { Name = "Kloon", Parent = mgs[0] });
            mgs.Add(new MembershipGroup { Name = "Desktop Team", Parent = mgs[1] });
            mgs.Add(new MembershipGroup { Name = "Web Team", Parent = mgs[1] });
            mgs.Add(new MembershipGroup { Name = "Mobile Team", Parent = mgs[1] });
            mgs.Add(new MembershipGroup { Name = "Pentalog", Parent = mgs[0] });
            mgs.Add(new MembershipGroup { Name = "Production", Parent = mgs[5] });
            mgs.Add(new MembershipGroup { Name = "Marketing", Parent = mgs[5] });
            mgs.Add(new MembershipGroup { Name = "Administration", Parent = mgs[5] });
            // Category
            var categories = new List<PersonCategory>
                                 {
                                     new PersonCategory {Name = "Employee"}, 
                                     new PersonCategory {Name = "Candidats"},
                                     new PersonCategory {Name = "Customers"},
                                     new PersonCategory {Name = "Users"}
                                 };
            mgs.ForEach(model.AddToMembershipGroups);
            categories.ForEach(model.AddToPersonCategories);

            model.SaveChanges();

            // People

            model.SaveChanges();

            for (int i = 0; i < 100; i++)
            {
                var p = new Person
                            {
                                Name = GetRandomText(),
                                PersonCategories = categories[Random.Next(categories.Count)]
                            };
                model.People.AddObject(p);
            }
            model.SaveChanges();

        }


        private static readonly Random Random = new Random();
        private static List<string> _sample;
        private static string GetRandomText()
        {
            if (_sample == null)
            {
                _sample = RES.NAMES.Split(new[] { "\r\n" }, StringSplitOptions.None)
                    .Select(i => i.Trim())
                    .Where(i => !string.IsNullOrEmpty(i) && !i.Contains(@"\") && !i.Contains('(') && !i.Contains(')')).ToList();
            }

            return _sample[Random.Next(_sample.Count)];
        }

    }
}
